<?php
include_once("functions/db_manipulate.php");
?>
    <meta charset="UTF-8">

<?php

connectDB();
startTransaction();

$tmpTopicCommentResultSet = getNewTopicCommentById($_GET['cid']);
$deleteTmpCommentResultSet = null;
$commentResultSet = null;

if ($tmpComment = mysql_fetch_assoc($tmpTopicCommentResultSet)) {

    $commentResultSet = mysql_query("INSERT INTO topicmessages(topicid, userid, content, date, time, quote)
                                     VALUES (
                                            '" . $tmpComment['topicid'] . "',
                                            '" . $tmpComment['userid'] . "',
                                            '" . $tmpComment['content'] . "',
                                            '" . $tmpComment['date'] . "',
                                            '" . $tmpComment['time'] . "',
                                            '" . $tmpComment['quote'] . "'
                                            )") or die(mysql_error());

    $idComment = mysql_insert_id();

    $imagesResultSet = getNewCommentImages($_GET['cid']);

    while ($image = mysql_fetch_assoc($imagesResultSet)) {
        $insertImageResultSet = mysql_query("INSERT INTO topicimages(path, topicmsgid) VALUES ('" . mysql_real_escape_string($image['path']) . "', '" . $idComment . "')") or die(mysql_error());
    }


    $deleteTmpCommentResultSet = mysql_query("DELETE FROM tmptopicmessages WHERE id = '" . $_GET['cid'] . "'") or die(mysql_error());
}

if ($tmpTopicCommentResultSet && $commentResultSet && $deleteTmpCommentResultSet) {
    commitTransaction();
} else {
    rollbackTransaction();
}

redirect($_SERVER['HTTP_REFERER']);